package service

import (
	"crypto/md5"
	"encoding/hex"
	"errors"
	"fmt"
	"goblog/global"
	"goblog/model"
)

/**
* @Author : zhoujin
* @Date : 2020/6/8 8:28 下午
 */

//真实定义对数据库操作的 某一个表的所有示例方法


func Register(u model.SysUser) (err error, uinter model.SysUser) {
	var user model.SysUser
	var p = &user
	var any interface{}
	any = "ligin"
	fmt.Println(p,any)
	isRegister := global.GLOBAL_DB.Where("username = ?",u.Username).First(&user).RecordNotFound()
	if !isRegister {
		return errors.New("已经注册"), u
	}else {
		 //创建
		err = global.GLOBAL_DB.Create(&u).Error
	}
	return err,u
	
}


func Login(u *model.SysUser)(err error,user *model.SysUser){
	var user model.SysUser
	md := md5.New()
	md.Write([]byte(u.Password))
	u.Password := hex.EncodeToString(md.Sum(nil))
	err := global.GLOBAL_DB.Where("username = ? AND password = ?",u.Username,u.Password).First(&user).Error
	if err != nil {
		 return err,&user
		 
	}

	err := global.GLOBAL_DB.Where("authority_id = ?",user.AuthorityId).First(&user.Authority).Error
	return err &user
}
